<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="文章标题" prop="goodName">
        <el-input v-model="queryParams.title" placeholder="请输入文章标题"/>
      </el-form-item>
      <el-form-item label="文章类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择文章类型" size="small">
          <el-option label="全部"
                     key="999"
                     value="-1"
          >
          </el-option>
          <el-option
            v-for="dict in articleType"
            :key=dict.dictValue
            :label="dict.dictLabel"
            :value=dict.dictValue
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="openAddOrUpdate"
          v-hasPermi="['shop:banner:add']"
        >添加
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="articleList">
      <el-table-column label="文章标题" align="center" prop="title"/>
      <el-table-column label="文章类型" :formatter="groupFormat" align="center" prop="type"/>
      <el-table-column label="文章标题图片" align="center" prop="image">
        <template slot-scope="scope">
          <el-image
            style="width: 50px; height: 50px"
            :src="scope.row.image"
            :preview-src-list="[scope.row.image]"
          >
          </el-image>
        </template>
      </el-table-column>

      <el-table-column label="发布者用户名" align="center" prop="author"/>
      <el-table-column label="是否显示" align="center" key="isHidden">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="0"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="openAddOrUpdate(scope.row.id)"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      :hide-on-single-page="true"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @size-change="sizeChange"
      @current-change="pageChange"
    />

  </div>
</template>

<script>
import * as articleApi from '@/api/shop/article'
import PictureManager from '@/components/ImageUpload/PictureManager'

export default {
  name: 'ShopBanner',
  components: {
    PictureManager
  },
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupId: null
      },
      showSearch: true,
      total: 0,
      articleList: [],
      articleType: [],
      loading: false
    }
  }
  ,
  computed: {},
  created() {
    this.getList()
    this.getDicts('article_type').then(response => {
      this.articleType = response.data
    })
  }
  ,
  methods: {
    getList() {
      articleApi.articleList(this.queryParams).then((res) => {
        this.total = res.total
        this.articleList = res.rows
      })
    }
    ,
    handleStatusChange(row) {
      let text = row.type === 1 ? '显示' : '隐藏'
      this.$confirm('确认要' + text + '本篇文章吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return articleApi.updateOrSave(row)
      }).then(() => {
        this.msgSuccess(text + '成功')
      }).catch(function() {
        row.type = row.type === 0 ? 1 : 0
      })
    }
    ,
    sizeChange(val) {
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = val
      this.getList()
    }
    ,
    pageChange(val) {
      this.queryParams.pageNum = val
      this.getList()
    }
    ,
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    }
    ,
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    }
    ,
    handleDelete(row) {
      this.$confirm('确认要删除本条广告吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return articleApi.deleteObj(row.id)
      }).then(() => {
        this.msgSuccess('删除成功')
        this.getList()
      }).catch(function() {
      })
    }
    ,
    openAddOrUpdate(id) {
      let param={
        path: '/shop/articleAdd'
      }
      if(typeof id === 'number'){
        param={
          path: '/shop/articleUpdate',
          query:{id}
        }
      }
      this.$router.push(param)
    },
    groupFormat(row) {
      return this.selectDictLabel(this.articleType, row.type)
    }
  }
}

</script>
<style lang="scss" scoped>

</style>
